﻿@using PagedList.Mvc
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Promotion
@model PromotionListViewModel
@{
    ViewBag.Title = "Promotions";
	Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

<div>
    @using (Html.BeginForm("Index", "Promotion", FormMethod.Get))
    {
        @Html.HiddenFor(m => m.SortOrder)
        <div id="promotions-list">
           
            <div class="table-responsive">
                
                @if (Model.Promotions.Any())
                {
                    <table class="table table-bordered tablesorter">
                        <thead>
                            <tr>
                                <th><a href="#">Promotion Name</a></th>
                                <th><a href="#">Discount</a></th>
                                <th><a href="#">Discount Unit</a></th>
                                <th><a href="#">Promo Code</a></th>
                                <th><a href="#">Effective Date</a></th>
                                <th><a href="#">Expiration Date</a></th>
                                <th><a href="#">Product</a></th>
                                <th><a href="#">Status</a></th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Promotions)
                            {
                               <tr>
                                    <td>@item.Name</td>
                                    <td>@item.Discount</td>
                                    <td>@(item.IsPercent?"Percent":"Dollar")</td>
                                    <td>@item.PromoCode</td>
                                    <td>@item.EffectiveDate.ToString("MM/dd/yyyy")</td>
                                    <td>@item.ExpirationDate.ToString("MM/dd/yyyy")</td>
                                    <td>@item.ProductText</td>
                                    <td>@(item.Status==0?"Pending":"")</td>
                                    <td>
                                        <a href="@Url.Action("Index", "EmailSend", new { discount = item.Discount, isPercent = item.IsPercent, promoCode = item.PromoCode, effectiveDate = item.EffectiveDate, expirationDate = item.ExpirationDate, product = item.Product })">Send</a>
                                        <a href="@Url.Action("AddOrEditPromo", "Promotion", new { id = item.ID})">Edit</a>
                                        <a href="#" class="remove-lnk" data-id="@item.ID">Delete</a>
                                        <a href="#" class="stop-lnk" data-id="@item.ID">Stop</a>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
      
                    @Html.PagedListPager(Model.Promotions, page => Url.Action("Index", new { page, sort = Model.SortOrder}))
                }
                else
                {
                    <p>No records found.</p>
                }
            </div>
        </div>
    }
</div>
    @using (Html.BeginForm("AddOrEditPromo", "Promotion", FormMethod.Post))
    {
        <button type="submit" class="btn btn-default">Add New Promo</button>
    }

<script type="text/javascript">
                $(function () {

                    $("#promotions-list table").tablesorter({
                        headers: {
                            8: { sorter: false }
                        }
                    });

                    var sortRefresh = true;
	                @if(!string.IsNullOrEmpty(Model.SortOrder))
                    {
                        <text>
	                sortRefresh = false;
	                setInitSort("promotions-list", '@(Model.SortOrder)');
                        </text>
                    }

	                $("#promotions-list table").on("sortEnd", function (event) {
	                    $("#SortOrder").val(event.target.config.sortList);
	                    if (sortRefresh)
	                        GetList();
	                    else
	                        sortRefresh = true;
	                });

	                $(".remove-lnk").click(function () {
	                    if (confirm('Are you sure you want to delete this promotion?')) {
	                        $.ajax({
	                            url: '@Url.Action("Delete", "Promotion")',
	                            type: "POST",
	                            data: { id: $(this).attr('data-id') },
	                            cache: false,
	                            success: function () {
	                                GetList();
	                            }
	                        });
	                    }
                        return false;
	                });

                    $(".stop-lnk").click(function () {
                        if (confirm('Are you sure you want to set promotion expired?')) {
                            $.ajax({
                                url: '@Url.Action("Stop", "Promotion")',
	                            type: "POST",
	                            data: { id: $(this).attr('data-id') },
	                            cache: false,
	                            success: function () {
	                                GetList();
	                            }
	                        });
                        }
                        return false;
                    });
	            });

                function GetList() {
                    var sort = $("#@Html.IdFor(m => m.SortOrder)").val();
                    var page = '@(Model.Promotions.PageNumber.ToString())';
                    window.location.href = "@Url.Action("Index")" + "?sort=" + sort + "&page=" + page;
                }
</script>
